package cn.itsource.basic.service.impl;

import cn.itsource.basic.mapper.BaseMapper;
import cn.itsource.basic.query.BaseQuery;
import cn.itsource.basic.service.IBaseService;
import cn.itsource.basic.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional(readOnly = true,propagation= Propagation.SUPPORTS)
public class BaseServiceImpl<T> implements IBaseService<T> {
    @Autowired
    private BaseMapper<T> baseMapper;
    @Override
    @Transactional(readOnly = false,propagation= Propagation.REQUIRED)
    public void save(T t) {
        baseMapper.save(t);
    }
    @Override
    @Transactional
    public void update(T t) {
        baseMapper.update(t);
    }
    @Override
    @Transactional
    public void delete(Long id) {
        baseMapper.remove(id);

    }
    @Override
    public T queryById(Long id) {
        return baseMapper.getById(id);
    }
    @Override
    public List<T> query() {
        return baseMapper.getAll();
    }
    @Override
    public PageResult<T> queryPage(BaseQuery query) {
        //1.查询总条数
        Long count = baseMapper.queryCount(query);
        List<T> list=null;
        if (count!=0||count!=null) {
            //2.查询页面的分页数据
            list = baseMapper.queryPage(query);
        }

       return new PageResult<>(count,list);
    }
}
